package InetAdress;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class InputAdress04 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(8888);
        System.out.println("等待连接！");
        Socket socket=serverSocket.accept();
        BufferedInputStream bufferedInputStream=new BufferedInputStream(socket.getInputStream());
        String pathname = "src\\xxx.png";
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(pathname));
        byte[] bytes=new byte[1024];
        int n=0;
        while ((n=bufferedInputStream.read(bytes))!=-1) {
            bufferedOutputStream.write(bytes,0,n);
        }
        socket.shutdownInput();

        //向客户端回复收到图片
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bufferedWriter.write("收到图片");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        //必须关闭流和socket

        /*bufferedOutputStream.close();*/
        bufferedWriter.close();
        serverSocket.close();
        socket.close();
        bufferedOutputStream.close();
        bufferedInputStream.close();

    }
}
